<template>
  <div class="page">
    <div class=" search margin-bottom-sm">
      <mt-cell title="箱号：" class="flex">
        <mt-field
          style="width:76%"
          placeholder="请输入箱号"
          v-model.trim="boxNo"
          ref="ref_1"
          @keyup.enter.native="queryAndprintWaybillLabel"
        ></mt-field>
        <mt-button
          style="width:24%"
          size="small"
          type="primary"
          @click="queryAndprintWaybillLabel"
          >查询</mt-button
        >
      </mt-cell>
      <mt-cell title="到仓件数：">
        <mt-field
          placeholder="请输入件数"
          ref="ref_2"
          v-model.trim="arrivalTotalPackages"
        ></mt-field>
      </mt-cell>
      <mt-cell title="仓库：">
        <el-select
          v-model="storageLocation"
          filterable
          size="mini"
          ref="ref_3"
          placeholder="请选择仓库"
        >
          <el-option
            v-for="(item, value) in storageLocationMap"
            :key="value"
            :label="item"
            :value="value"
          />
        </el-select>
      </mt-cell>
      <mt-cell title="库区：" class="flex">
        <mt-field
          style="width:76%"
          placeholder="请输入库区"
          ref="ref_4"
          v-model.trim="warehouseParam.number"
        ></mt-field>
        <mt-button
          style="width:24%"
          size="small"
          type="primary"
          @click="saveArrivalTotalPackages"
          :disabled="isDisable"
          >保存</mt-button
        >
      </mt-cell>
    </div>
    <div>
      <mt-cell title="运单号：" :value="waybillNo"></mt-cell>
      <mt-cell title="客户：" :value="customerName"></mt-cell>
      <mt-cell title="国家：" :value="country"></mt-cell>
      <mt-cell title="入库渠道：" :value="inStockName"></mt-cell>
      <mt-cell title="客户件数：" :value="customerCount"></mt-cell>
      <mt-cell title="到仓件数：" :value="arrivalPackages"></mt-cell>
      <mt-cell title="订单状态：" :value="statusName"></mt-cell>
      <mt-cell title="销售：" :value="saleName"></mt-cell>
      <mt-cell title="耗时（毫秒）：" :value="time"></mt-cell>
    </div>
  </div>
</template>
<script>
import { ajax } from "@/store/yg/common/utils";
import axios from "axios";
import * as constant from "@/store/yg/common/constant";
import { Toast } from "mint-ui";
import { m } from "@/store/yg/common/local";

export default {
  components: {},
  props: [],
  data() {
    return {
      boxNo: "",
      arrivalTotalPackages: "",
      waybillNo: "",
      customerName: "",
      inStockName: "",
      customerCount: "",
      arrivalPackages: "",
      customerWay: "",
      statusName: "",
      saleName: "",
      time: "",
      waybillId: "",
      reservoirAreaShow: "",
      storageLocation: "",
      storageLocationMap: {},
      isDisable: false,
      warehouseParam: {
        number: ""
      }
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.initStorageLocations();
  },
  methods: {
    initStorageLocations: function() {
      ajax("storageLocations", {},{ type: "mobile" },{ auth: true }).then(
        res => {
          console.log(res);
          if (res.code == 200) {
            this.storageLocationMap = res.data;
          }else{
            Toast("操作失败"+res.message);
          }
        },
        error => {
          console.log(error);
        }
      );
    },
    queryAndprintWaybillLabel() {
      if (!this.boxNo) {
        Toast("请输入箱号");
        return;
      }
      if (this.boxNo.length<6) {
        Toast("请输入正确的箱号");
        return;
      }
      ajax(
        "doCargoTrace",
        { boxNo: this.boxNo, isPrint: false },
        { type: "mobile" },{ auth: true }
      ).then(
        res => {
          console.log(res);
          if (res.code == 200) {
            var data = res.data;
            this.waybillNo = data.waybillNo;
            this.customerName = data.customerName;
            this.inStockName = data.inStockName;
            this.customerCount = data.customerCount;
            this.arrivalPackages = data.arrivalPackages;
            this.customerWay = data.customerWay;
            this.statusName = m(data.statusName);
            this.saleName = data.saleName;
            this.time = data.time;
            this.waybillId = data.waybillId;
            this.reservoirAreaShow = data.reservoirArea;
          } else {
            Toast("操作失败"+res.message);
          }
        },
        error => {
          console.log(error);
          Toast("登录时效过期");
          this.$router.push('/login');
        }
      );
    },
    async saveArrivalTotalPackages() {
      this.isDisable=true
      if (!this.waybillId) {
        this.$message.error("运单不存在");
        return;
      }
      if (!this.arrivalTotalPackages) {
        this.arrivalTotalPackages = this.customerCount;
      }else{
        if (this.customerCount&&parseInt(this.customerCount)<parseInt(this.arrivalTotalPackages)){
          this.$message.error("录入到仓件数之和不能大于下单件数")
          return;
        }
      }
      if(this.arrivalPackages&&this.customerCount&&parseInt(this.customerCount)<parseInt(this.arrivalTotalPackages)+parseInt(this.arrivalPackages)){
        Toast("录入到仓件数之和不能大于下单件数")
        return;
      }
      let res = await ajax("findByWarehouse", this.warehouseParam, {
        type: "mobile"
      });
      console.log(res);
      if (res.code != 200 ) {
        return this.$message.error("您输入库区不存在！");
      }

      ajax(
        "saveArrivalTotalPackages",
        {
          waybillId: this.waybillId[0],
          arrivalTotalPackages: this.arrivalTotalPackages,
          reservoirArea: this.warehouseParam.number,
          storageLocation: this.storageLocation,
          customerCount: this.customerCount
        },
        {
          type: "mobile"
        }
      ).then(
        res => {
          console.log(res);
          if (res.code == 200) {
            this.boxNo = "";
            this.arrivalPackages = "";
            this.warehouseParam.number = "";
            this.isDisable=false;
            Toast({
              message: "保存成功",
              iconClass: "icon icon-success"
            });
            /*this.timeout = setTimeout(() => {
              window.location.reload(true);
            }, 1000 * Math.random());*/
          } else {
            Toast(res.message);
            this.isDisable=false;
          }
        },
        error => {
          console.log(error);
          this.isDisable=false;
        }
      );
    }
  }
};
</script>
<style></style>
<style scoped>
.page {
  background: #f5f5f6;
}
.page >>> .search .mint-cell-value {
  flex: 3;
}
</style>
